package com.cn.wanxi.IO;

import com.cn.wanxi.IOModel.CompanyIOModel;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.ReadNote;
import com.cn.wanxi.util.Tool;

import java.io.*;


public class CompanyIO {
    /**
     * 通过IO流来得到数据
     * 1.要把数据存入IO流，存入记事本
     * 2.将记事本的数据读取出来
     * 3.将读取出来的字符串进行封装成需要的对象或者集合
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
        String path = Tool.COMPANYPATH.getPath();//将数据存入到具体地方
        String split = Tool.SPLIT.getPath();
//1.存
        writeCompany(path, split);

//        2.读
        String result = ReadNote.readCompany(path);
//        3.封装
//        CompanyModel companyModel = new CompanyModel();
//        companyModel = setCompanyModel(result);
//        return companyModel;
        return setCompanyModel(result, split);
    }

    private CompanyModel setCompanyModel(String result, String split) {
        String[] companyModels = result.split(split);
        for (int i = 0; i < companyModels.length; i++) {
            System.out.println(i + "-----------" + companyModels[i]);
        }
        CompanyModel companyModel = new CompanyModel();
        companyModel.setLogo(companyModels[0]);
        companyModel.setPhone(companyModels[1]);
        companyModel.setAd(companyModels[2]);
        companyModel.setNet(companyModels[3]);
        companyModel.setLinePhone(companyModels[4]);
        companyModel.setAddress(companyModels[5]);
        companyModel.setVersion(companyModels[6]);
        companyModel.setIntroduction(companyModels[7]);
        return companyModel;
    }


    private void writeCompany(String path, String split) {

        File file = new File(path);//将地址加载到类里面
        boolean isExists = file.exists();
//        if (!isExists) {
        try {
            file.createNewFile();//捕获异常

            OutputStream outputStream = new FileOutputStream(file);
            CompanyIOModel companyIOModel = new CompanyIOModel();
            String logo = companyIOModel.getLogo();
            outputStream.write(logo.getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getPhone().getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getAd().getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getEmail().getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getLinePhone().getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getAddress().getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getVersion().getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(companyIOModel.getIntroduction().getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //        }
    }

    public static void main(String[] args) {
        new CompanyIO().getCompanyModel();
//        CompanyIO.getCompanyModel();
    }

}
